<?PHP
namespace SDF\Lib\Goods;

//use MongoDB\BSON\UTCDateTime;
//use MongoDB\Driver\WriteConcern;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\WriteConcern;
use SDF\Db\MongoDb;

/**
 * Class goods 商品
 * @package SDF\Lib\Goods
 */
class goods extends MongoDb {
    /**
     * 授权构造
     * special_auth constructor.
     */
    public function __construct(){
        parent::__construct('goods', 'goods');
    }

    /**
     * 添加商品
     * @param $document
     * @param array $options
     * @param WriteConcern|NULL $writeConcern
     * @return array
     * @throws \Exception
     */
    public function insertOne(&$document, array $options = [], WriteConcern $writeConcern = NULL) {
        if (empty($document['goods_title'])){
            throw new \Exception('goods_title is empty');
        }
        if (empty($document['specification'])){
            throw new \Exception('specification is empty');
        }
        if (empty($document['price'] or !is_int($document['price']))){
            throw new \Exception('price is empty or no int');
        }
        if (!is_array($document['tags_arr'] )){
            throw new \Exception('tags_arr not array');
        }
        if (isset($document['sort']) and !is_int($document['sort'] )){
            throw new \Exception('sort not int');
        }

        $data = array(
            "goods_title" => $document['goods_title'],//商品标题
            "goods_sub_title" => empty($document['goods_sub_title'])?'':$document['goods_sub_title'],//子标题，红色醒目标注商品。通常用于各类活动和对商品的详细描述。
            "specification" => $document['specification'],//计量单位
            "price" => intval($document['price']),
            "original_price" => intval($document['original_price']),
            "tags_arr" => $document['tags_arr'],
            "sort" => $document['sort'],
            "barcode" => "6935145301078",
            "sku_id" => "010302_0009",
            "on_sale" => 1,
            "goods_category_id" => new ObjectID("56ad9512f63d99b94f8bb383"),//一个商品只能归属某一个类目
            "goods_category_id_arr" => [],//所属类目列表，从3级到顶级类目的id列表。
            "goods_detail" => '',//商品的详细描述。
            "is_bargain" => 0,//特价商品标记，被标为特价的商品在商品标题旁边都应该有“特价”标签显示，特别是在商城系统里面，因为特价和烟酒不能参与一些活动的。特价商品不参与满减，代金券等优惠活动，0:或者不存在此属性，表示非特价商品，1:表示特价商品。
            "is_hot" => 0,//热门商品标记
            "is_pre_sale" => 0,//是否预售
            "goods_pic" => "http=>//img.ismbao.com/upload/201603/23/1803_56f269f1b8fb8.jpg",//商品主图
            "goods_imgs" => [//商品图片列表。
                "http=>//img.ismbao.com/upload/201603/23/1805_56f26a7aa4248.jpg",//排到第一位的为主图
                "http=>//img.ismbao.com/upload/201603/23/1803_56f269f1df3aa.jpg",
                "http=>//img.ismbao.com/upload/201603/23/1803_56f269f1b8fb8.jpg",
                "http=>//img.ismbao.com/upload/201603/23/1805_56f26a7ab95f5.jpg"
            ],
            "property" => array(),//商品属性
            "create_time" => new UTCDateTime(),
            "update_time" => new UTCDateTime(),
            "units" => 1,//默认规格
            "specification_transfer" => 1,//调货规格
            "specification_procurement" => 1,//采购规格
            "sales_volume_reality" => 0,//真实销量（通过订单售出，并配送完成的数量，不含退换的数量），这个数量需要定时从统计模块读取更新。
            "sales_volume" => 0,//已售出量，给顾客看的销售数量，后台可以任意设置
            "last_shelf_life" => 365,//商品保质期天数
            "last_price" => 875,//最后一次采购的单价
            "cost" => 912.79,//平均入库单价
            "barcode_arr" => ["6935145301870", "6935145301078"],//条码列表
            "specification_arr" => [
                "24" => [
                    "units" => 24,//关联计量单位的数量
                    "specification" => "件",//规格名称
                    "price" => 28000,//销售的价格
                    "original_price" => 30720,
                    "barcode" => "6935145301870",//规格条码，多个条码用逗号分开
                    "last_price" => 21000,//最后采购单价
                    "on_sale" => "0",//是否可售 1：可售 0:禁售
                    "specification_pic" => "http://img.ismbao.com/upload/201603/23/1805_56f26a7ab95f5.jpg"//规格对应的图片
                ],
                "1" => [
                    "units" => 1,
                    "specification" => "瓶",
                    "price" => 1350,
                    "original_price" => 1500,
                    "barcode" => "6935145301078",
                    "last_price" => 875.0,
                    "on_sale" => 1,
                    "specification_pic" => "http=>//img.ismbao.com/upload/201603/23/1803_56f269f1b8fb8.jpg"
                ]
            ],
            "goods_type" => 1,//商品类型 2:新品，1:正常，10:清仓（需要录入清仓原因），9:淘汰，15:赠品
            "send_type" => 1,//送达类型 1:30分内送达,2:次日送达
            "is_purchase_type" => "5",//清仓原因 1:涨价,2:滞销,3:缺货,4:淡季,5:清仓,6:其他
            "is_top" => 0,//是否推荐到首页
            "turn_day" => 95.0,//月均销量
            "total_sales" => 244,//总销售额
            "auto_shelves" => 1,//是否自动上下架
            "min_order_num" => 1,//最小购买量
            "no_packing" => 0,//是否需要打包
            "return_goods" => 0
        );
        parent::insertOne($data, $options, $writeConcern);
        return $data;
    }

    /**
     * 通过_id数组删除商品
     * @param $id_arr
     * @return \MongoDB\Driver\WriteResult
     */
    public function deleteById($id_arr){
        $filter = [
            '$in'=>array('_id'=>$id_arr),
        ];
        return parent::deleteMany($filter);
    }

    public function deleteMany($filter, array $options = [], WriteConcern $writeConcern = NULL) {
        throw new \Exception('商品禁止删除，你可以通过淘汰的方式让商品不出现在管理列表中');
    }
    public function deleteOne($filter, array $options = [], WriteConcern $writeConcern = NULL) {
        throw new \Exception('商品禁止删除，你可以通过淘汰的方式让商品不出现在管理列表中');
    }

    /**
     * 淘汰商品
     * @param $_id
     * @return \MongoDB\Driver\WriteResult
     */
    public function obsolescenceById($_id){
        //todo 有库存的商品不能淘汰
        //todo 有冻结库存的不能淘汰
        //todo 未下架的商品不能淘汰
        $filter = array(
            '_id'=>self::format($_id)
        );
        return $this->updateOne($filter,array(
            'goods_type' => 9,
        ));
    }

}